/*
 * This file is part of aion-engine <aion-engine.com>
 *
 * aion-engine is private software: you can redistribute it and or modify
 * it under the terms of the GNU Lesser Public License as published by
 * the Private Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * aion-engine is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser Public License for more details.
 *
 * You should have received a copy of the GNU Lesser Public License
 * along with aion-engine.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionengine.gameserver.spawnengine;

import com.aionengine.gameserver.controllers.StaticObjectController;
import com.aionengine.gameserver.dataholders.DataManager;
import com.aionengine.gameserver.model.gameobjects.StaticDoor;
import com.aionengine.gameserver.model.gameobjects.VisibleObject;
import com.aionengine.gameserver.model.templates.spawns.SpawnGroup2;
import com.aionengine.gameserver.model.templates.spawns.SpawnTemplate;
import com.aionengine.gameserver.model.templates.staticdoor.DoorType;
import com.aionengine.gameserver.model.templates.staticdoor.StaticDoorTemplate;
import com.aionengine.gameserver.model.templates.staticdoor.StaticDoorWorld;
import com.aionengine.gameserver.utils.idfactory.IDFactory;
import com.aionengine.gameserver.world.World;
import com.aionengine.gameserver.world.geo.GeoService;
import com.aionengine.gameserver.world.knownlist.PlayerAwareKnownList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author MrPoke
 */
public class StaticDoorSpawnManager {

    private static Logger log = LoggerFactory.getLogger(StaticDoorSpawnManager.class);

    /**
     * @param spawnGroup
     * @param instanceIndex
     */
    public static void spawnTemplate(int worldId, int instanceIndex) {
        StaticDoorWorld staticDoorWorld = DataManager.STATICDOOR_DATA.getStaticDoorWorlds(worldId);
        if (staticDoorWorld == null)
            return;
        int counter = 0;
        for (StaticDoorTemplate data : staticDoorWorld.getStaticDoors()) {
            if (data.getDoorType() != DoorType.DOOR) {
                // TODO: assign house doors to houses, so geo doors could be triggered by changing house settings;
                // The same for abyss doors, they need to have owners.
                continue;
            }
            SpawnTemplate spawn = new SpawnTemplate(new SpawnGroup2(worldId, 300001), data.getX(), data.getY(), data.getZ(), (byte) 0, 0, null, 0, 0);
            spawn.setStaticId(data.getDoorId());
            int objectId = IDFactory.getInstance().nextId();
            StaticDoor staticDoor = new StaticDoor(objectId, new StaticObjectController(), spawn, data, instanceIndex);
            staticDoor.setKnownlist(new PlayerAwareKnownList(staticDoor));
            bringIntoWorld(staticDoor, spawn, instanceIndex);
            if (staticDoor.getDoorName() != null) {
                GeoService.getInstance().setDoorState(worldId, instanceIndex, staticDoor.getDoorName(), staticDoor.isOpen());
            }
            counter++;
        }
        if (counter > 0)
            log.info("Spawned static doors: " + worldId + " [" + instanceIndex + "] : " + counter);
    }

    /**
     * @param visibleObject
     * @param spawn
     * @param instanceIndex
     */
    private static void bringIntoWorld(VisibleObject visibleObject, SpawnTemplate spawn, int instanceIndex) {
        World world = World.getInstance();
        world.storeObject(visibleObject);
        world.setPosition(visibleObject, spawn.getWorldId(), instanceIndex, spawn.getX(), spawn.getY(), spawn.getZ(),
                spawn.getHeading());
        world.spawn(visibleObject);
    }
}
